//
//  FindViewController.m
//  ShandsMod
//
//  Created by zh s on 2023/3/22.
//  Copyright © 2023 zh_s. All rights reserved.
//

#import "FindViewController.h"

#import "FindHeaderV.h"
#import "FindListCell.h"

@interface FindViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSArray *dataArr;
/**! historyArr*/
@property (nonatomic, strong) NSMutableArray *historyArr;

@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav.hidden = YES;
    
    FindHeaderV *headerV = [[FindHeaderV alloc] init];
    [self.view addSubview:headerV];
    headerV.frame = CGRectMake(0, 0, SCREEN_WIDTH,  STATUS_BAR_HEIGHT  +64);
    headerV.backgroundColor = UIColor.whiteColor;
    
    WS(weakSelf);
    headerV.findSearchViewBlock = ^(NSString * _Nonnull searchStr) {
        [weakSelf didSelctedSearch:searchStr];
    };
    [self data_init];
    [self UI_init];
    
    // Do any additional setup after loading the view.
}

#pragma mark ----- UI_init
- (void)UI_init {
    [self.view addSubview:self.mainTableView];
}
#pragma mark ----- data_init
- (void)data_init {
//    _historyArr = [NSMutableArray array];
    _dataArr = @[@"",@"",@""];
}
#pragma mark ----- tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.historyArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FindListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.arrowImg.tag = indexPath.section;
    cell.titleLab.text = self.historyArr[indexPath.section];
    [cell.arrowImg addTapGestureTarget:self action:@selector(deletedCell:)];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == self.dataArr.count - 1) {
        return 40;
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == self.dataArr.count - 1) {
        UIView *fotterV = [BaseViewFactory viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) color:UIColor.clearColor];
        UILabel *tipsLab = [BaseViewFactory labelWithFrame:CGRectMake(0, 15, SCREEN_WIDTH, 20) textColor:TEXT_COLOR_170 font:FONT_NAME(FONT_Regular, 10) textAligment:NSTextAlignmentCenter andtext:@"仅展示七天内记录"];
        [fotterV addSubview:tipsLab];
        return fotterV;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (void)deletedCell:(UIGestureRecognizer *)ges {
    NSInteger tag = [ges view].tag;
    
    NSMutableArray *removeCityArr = [NSMutableArray array];
//    NSMutableArray *cityArr = [GWUserDefaults objectForKey:SEARCH_HISTORY_KEY];
    
    [self.historyArr removeObjectAtIndex:tag];
    [self.mainTableView reloadData];
    return;
    [self.historyArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [removeCityArr addObject:obj];
    }];
    if (removeCityArr.count >0) {
        //1.创建UIAlertControler
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认清空全部历史记录？" preferredStyle:UIAlertControllerStyleAlert];
        //2.1 确认按钮
        UIAlertAction *conform = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        UIAlertAction *conform1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [removeCityArr removeAllObjects];
            
            [GWUserDefaults setValue:removeCityArr forKey:SEARCH_HISTORY_KEY];
            [GWUserDefaults synchronize];
            [self.historyArr removeAllObjects];
            [UIView setAnimationsEnabled:NO];
            [self.mainTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
            [UIView setAnimationsEnabled:YES];
        }];
        if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_9_0) {
            [conform setValue:GRAYTEXT_COLOR forKey:@"titleTextColor"];
            [conform1 setValue:YELLOWER_COLOR forKey:@"titleTextColor"];
        }
        [alert addAction:conform];
        [alert addAction:conform1];
        
        //4.显示弹框
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
}

- (void) didSelctedSearch:(NSString *)searchStr
{
    if (IsStrEmpty(searchStr)) {
        return;
    }

    if (self.historyArr.count >= 10000) {
        [self.historyArr removeLastObject];
    }
    for (NSString *str in self.historyArr) {
        if ([searchStr isEqualToString:str]) {
            [self.historyArr removeObject:str];
            break;
        }
    }
    [self.historyArr insertObject:searchStr atIndex:0];
    [[NSUserDefaults standardUserDefaults] setValue:self.historyArr forKey:SEARCH_HISTORY_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [self.mainTableView reloadData];
}

#pragma mark ----- 懒加载
- (UITableView *)mainTableView {
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT + 64, SCREEN_WIDTH, SCREEN_HEIGHT -STATUS_BAR_HEIGHT - 45) style:UITableViewStyleGrouped];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.rowHeight = 33;//iOS8之后默认就是这个值，可以省略
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _mainTableView.backgroundColor = BASE_BACKCOLOR;
        [_mainTableView registerClass:[FindListCell class] forCellReuseIdentifier:@"cell"];
    }
    return _mainTableView;
}
- (NSMutableArray *) historyArr
{
    if (_historyArr == nil) {
        NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:SEARCH_HISTORY_KEY];
        _historyArr = (array == nil ? [[NSMutableArray alloc] init] : [[NSMutableArray alloc] initWithArray:array copyItems:YES]);
    }
    return _historyArr;
}

@end
